<?php
include 'headers.php';
//print_r($_POST);
//die();
//Array ( [id] => 19 [wordSearch] => สิน [product_status] => 0 [category_id] => 0 [trade_id] => 0 [province_id] => 0 )

$sql = 'SELECT * FROM products WHERE name like "%' . $db->antihack($_POST['wordSearch']) . '%" ';

if($_POST['product_status'] != 0)
{
    $sql .='AND product_status = ' . $db->antihack($_POST['product_status']) . ' ';
}

if($_POST['category_id'] != 0)
{
    $sql .='AND category_id = ' . $db->antihack($_POST['category_id']) . ' ';
}

if($_POST['trade_id'] != 0)
{
    $sql .='AND trade_id = ' . $db->antihack($_POST['trade_id']) . ' ';
}

if($_POST['province_id'] != 0)
{
    $sql .='AND province_id = ' . $db->antihack($_POST['province_id']) . ' ';
}

$sql .= 'AND id < ' . $db->antihack($_POST['id']) . ' ORDER BY id DESC LIMIT 10 ';
$res = $db->select_query($sql);

//$res = $db->select_query('SELECT id, name FROM products WHERE name like "%'. $db->antihack($_POST['wordSearch']) .'%" AND id < '. $db->antihack($_POST['id']) .' ORDER BY id DESC LIMIT 10');
$countRow = mysql_num_rows($res);
if($countRow != 0)
{
    while($arr = $db->fetch($res))
    {
        $countReply = $db->num_rows('product_reply', 'reply_id', 'product_id = ' . $arr['id'] . '');
        ?>     
        <li data-ctype="marketSearch" data-wordsearch="<?php echo $_POST['wordSearch']; ?>" id="market_<?php echo $arr['id']; ?>" last_id="<?php echo $arr['id']; ?>" data-productstatus="0" data-categoryid="0" data-tradeid="0" data-provinceid="0" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-li-has-count ui-li-has-thumb ui-btn-up-c"><div class="ui-btn-inner ui-li"><div class="ui-btn-text">
                    <a rel="external" href="?q=productDetail&pid=<?php echo $arr['id']; ?>" class="ui-link-inherit">
                        <img src="<?php echo $arr['tn_part']; ?>" alt="" width="80" height="80" class="ui-li-thumb">
                        <h3 class="ui-li-heading"><?php echo $arr['name']; ?></h3>
                        <p class="ui-li-desc"><?php echo $arr['headline']; ?></p>
                        <span class="ui-li-count ui-btn-up-c ui-btn-corner-all"><?php echo $countReply; ?></span>
                    </a> 
                </div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></div>
        </li>                
        <?php
    }
}
else
{
    echo '0';
}
?>